package com.eat.fish_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eat.common_utils.R;
import com.eat.fish_service.entity.Dao.Expert;
import com.eat.fish_service.entity.Dao.User;
import com.eat.fish_service.entity.ExpertCheck;
import com.eat.fish_service.mapper.ExpertCheckMapper;
import com.eat.fish_service.mapper.ExpertMapper;
import com.eat.fish_service.service.ExpertCheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eat.fish_service.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.eat.fish_service.webSocket.WebSocketServer;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author WZX
 * @since 2022-08-01
 */
@Service
public class ExpertCheckServiceImpl extends ServiceImpl<ExpertCheckMapper, ExpertCheck> implements ExpertCheckService {

    @Autowired
    private ExpertMapper expertMapper;

    @Autowired
    private WebSocketServer webSocketServer;

    @Autowired
    private UserService userService;

    @Override
    public R addExpertCheck(ExpertCheck expertCheck) {
        int result = baseMapper.insert(expertCheck);
        if (result == 0) {
            return R.error();
        }
        return R.ok();
    }

//    @Override
//    public R getAllExpertCheck() {
//        List<ExpertCheck> list = baseMapper.selectList(null);
//        return R.ok().data("list", list);
//    }

//    @Override
//    public R passExpertCheck(ExpertCheck expertCheck) {
//        expertCheck.setIsPass(1);
//        baseMapper.updateById(expertCheck);
//        Expert expert = new Expert();
//        BeanUtils.copyProperties(expertCheck, expert);
//        int insert = expertMapper.insert(expert);
//        if (insert == 0) {
//            return R.error().message("无法通过审核");
//        }
//        webSocketServer.newsCount();   //消息提醒管理员有新的审核
//        return R.ok().message("已通过审核");
//    }

    @Override
    public R apply(ExpertCheck expertCheck) {
        ExpertCheck expertCheckFromDB = baseMapper.selectById(expertCheck.getId());
        //如果该用户还没申请，就添加，否则更新

        if (expertCheckFromDB == null) {
            baseMapper.insert(expertCheck);
        } else {
            expertCheck.setIsPass(0);
            baseMapper.updateById(expertCheck);
        }
        webSocketServer.newsCount();  //更新管理员要审核的消息
        return R.ok().message("已提交审核");
    }

    @Override
    @Transactional
    public R result(String id, Integer isPass, String reason) {
        ExpertCheck expertCheck = baseMapper.selectById(id);
        int update;
        if (isPass == 1) { //审核通过
            expertCheck.setIsPass(1);
            Expert expert = new Expert(expertCheck.getId(),
                    expertCheck.getRealName(),
                    expertCheck.getQualification1(),
                    expertCheck.getQualification2(),
                    expertCheck.getQualification3(),
                    expertCheck.getIntroduce());
            expertMapper.insert(expert);

            //用户表更改status
            User updateUser = new User();
            updateUser.setId(id);
            updateUser.setStatus(2);
            userService.updateById(updateUser);

        } else {  //审核不通过
            expertCheck.setIsPass(2);
            expertCheck.setReason(reason);
        }
        update = baseMapper.updateById(expertCheck);
        webSocketServer.newsCount();   //消息提醒管理员有新的审核

        if (update == 1) {
            return R.ok();
        } else {
            return R.error().message("审核失败！");
        }
    }

    @Override
    public R queryMyApply(String id) {
        ExpertCheck expertCheck = baseMapper.selectById(id);
        return R.ok().data("expertCheck", expertCheck);
    }


    @Override
    public R queryCheckList() {
        List<ExpertCheck> expertCheckList = baseMapper.selectList(new QueryWrapper<ExpertCheck>().eq("is_pass", 0));
        return R.ok().data("expertCheckList", expertCheckList);
    }

    @Override
    public R getOneExpertCheck(String id) {
        ExpertCheck expertCheck = baseMapper.selectById(id);
        return R.ok().data("expertCheck", expertCheck);
    }

    @Override
    public int checkSize() {
        Integer count = baseMapper.selectCount(new QueryWrapper<ExpertCheck>().eq("is_pass", 0));
        return count;
    }
}
